package com.hd.commons.shiro.filter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.filter.PathMatchingFilter;
import org.apache.shiro.web.util.WebUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * hzhh123
 * 2019/5/10 17:30
 * <p>
 * 拦截每一个请求
 * </p>
 */
public class SyncOnlineSessionFilter extends PathMatchingFilter {
    private static final Logger logger= LoggerFactory.getLogger(SyncOnlineSessionFilter.class);
    @Override
    protected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
        logger.info("开始拦截。。。。。。。。。。。");
        logger.info("请求地址：{}", WebUtils.getPathWithinApplication(WebUtils.toHttp(request)));
        HttpServletRequest req=(HttpServletRequest)request;
        logger.info("sessionId={}",req.getSession().getId());
        logger.info("lasttime={}",req.getSession().getLastAccessedTime());
        logger.info("createTime={}",-req.getSession().getCreationTime());
        HttpSession session=req.getSession();
        long d=(req.getSession().getLastAccessedTime()-req.getSession().getCreationTime())/1000;
        logger.info("d={}",d);
        System.out.println("测试debug模式");
        //判断是否登录
        boolean isLogin=SecurityUtils.getSubject().isAuthenticated();
        if(isLogin){
            //登录
            //SecurityUtils.getSubject().logout();
        }
        System.out.println(isLogin);
        return true;
    }
}
